package com.liwenfeng.ui;

import java.util.function.BiFunction;
import java.util.function.BiPredicate;

public class MonteCarlo {
    public <R> R monteCarlo(Integer num, BiPredicate<Double, Double> predicate, BiFunction<Integer, Integer, R> function) {
        int count = 0;
        for (int i = 0; i < num; i++) {
            double x = Math.random();
            double y = Math.random();
            if (predicate.test(x, y)) {
                count += 1;
            }
        }
        return function.apply(count, num);
    }
}
